package com.hnu.web;

import com.hnu.model.User;

public class EditUserInfoBean extends BaseAction {
	private static final long serialVersionUID = 1900488845266617885L;
	private User user;
	private String message = "";
	private String password;
	private String repeatPassword;

	public String toEditUserInfo() {
		long userId = ((User) this.session.get("user")).getUserId();
		this.user = this.servLocator.getUserServ().getUserByUserId(userId);
		return "success";
	}

	public String editUserInfo() {
		User temp = (User) this.session.get("user");
		this.user.setPassword(temp.getPassword());
		
		if((this.password != null) && (!this.password.trim().isEmpty()))
		{
			if(!this.password.equals(this.repeatPassword))
			{
				this.message = "两次密码输入必须一致！";
				return "failure";
			}
			else
			{
				this.user.setPassword(password);
			}
		}
		if((this.repeatPassword != null) && (!this.repeatPassword.trim().isEmpty()))
		{
			if(!this.repeatPassword.equals(this.password))
			{
				this.message = "两次密码输入必须一致！";
				return "failure";
			}
			else
			{
				this.user.setPassword(password);
			}
		}
		if(this.user.getEmail() == null || this.user.getEmail().trim().isEmpty())
		{
			this.message = "邮件不能为空！";
			return "failure";
		}
		if(!this.user.getEmail().matches("[0-9a-z_]+@(([0-9a-z]+)[.]){1,2}[a-z]{2,3}"))
		{
			this.message = "邮件格式不符！";
			return "failure";
		}
		
		user.setPassword(user.getPassword().trim());
		user.setUserName(user.getUserName().trim());
		
		this.servLocator.getUserServ().updateUser(this.user);
		this.message = "修改成功!";
		return "success";
	}

	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getMessage() {
		return message;
	}

	public String getRepeatPassword() {
		return repeatPassword;
	}

	public void setRepeatPassword(String repeatPassword) {
		this.repeatPassword = repeatPassword;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

}
